#include "hexitem.h"
#include <QPointF>
#include <QVector>
#include <math.h>
#include <QBrush>
#include <QToolTip>
#include <QHelpEvent>
#include <QGraphicsSceneHoverEvent>
#include <cstdio>

HexItem::HexItem(QGraphicsPolygonItem *parent, const int size, const double h) : QGraphicsPolygonItem(parent)
{
    this->size = size;
    this->h = h;
    QPolygonF polygon;
    polygon << QPointF(2*size,0) << QPointF(size,-h) << QPointF(-size,-h) << QPointF(-2*size,0) << QPointF(-size,h) << QPointF(size,h);
    setPolygon(polygon);
    setAcceptsHoverEvents(true);
    setFlags(QGraphicsItem::ItemIsFocusable);
    circle = NULL;
}

void HexItem::setMeadow(Meadow *meadow, int i, int j)
{
    this->meadow = meadow;
    this->i = i;
    this->j = j;
}

int HexItem::getX()
{
   return i;
}

int HexItem::getY()
{
   return j;
}

int HexItem::getColor()
{
    double displaySizeFactor = (255.0 - 50.0) / (10500.0 - 0.0);
    double displaySizeConstant = 255.0 - 10500.0 * displaySizeFactor;
    //printf("%lf : %lf\n",displaySizeFactor, displaySizeConstant);
    return meadow->getField(i,j) * displaySizeFactor + displaySizeConstant;
}

void HexItem::paint()
{
    int grass = getColor();
    //printf("%d\n",grass);
    setPen(QPen(Qt::black, 0));
    setBrush(QBrush(QColor(255-grass,255,255-grass)));
    update(boundingRect());
}

QRectF HexItem::boundingRect() const
{
    return QRectF(QPointF(-2 * this->size,-this->h),QSizeF(4 * this->size, 2 * this->h));
}

int HexItem::type()
{
    return 77;
}
